﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaseArith {
    public static class Search {
        /// <summary>
        /// 输入数组需要单调增
        /// </summary>
        /// <param name="target"></param>
        /// <param name="orderarray"></param>
        /// <returns></returns>
        public static int BinarySearch(int target,int[] orderarray) {
            var p = default(int);
            var mid = orderarray.Length / 2;
            var mark = orderarray[mid];
            if (mid == 0 || target == mark) {
                p += mid;
            } else if (target > mark) {
                p += mid;
                var bigorderarray = (from item in orderarray where item > mark select item).ToArray();
                p += BinarySearch(target,bigorderarray);
            } else {
                var smallorderarray = (from item in orderarray where item < mark select item).ToArray();
                p += BinarySearch(target,smallorderarray);
            }
            return p;
        }
    }
}
